var dateUtils = {
  /**
   * 获取当前 周次 及 当前周次的开始结束时间
   */
  getCurrentWeek() {
    let now = new Date();
    const dayOfWeek = now.getDay(); // 0 表示周日，1 到 6 表示周一到周六
    const startDate = new Date(now);
    const endDate = new Date(now);

    // 调整为周一
    startDate.setDate(startDate.getDate() - dayOfWeek + 1);
    // 调整为周日，即下周一减去一天
    endDate.setDate(endDate.getDate() - dayOfWeek + 7);

    // 获取当前周数
    const year = now.getFullYear();
    let firstDayOfYear = new Date(year, 0, 1);
    let diffMilliseconds = now.getTime() - firstDayOfYear.getTime();
    let currentWeek = Math.ceil(diffMilliseconds / (7 * 24 * 60 * 60 * 1000));
    const weekNum: number = currentWeek;

    // 返回周数、开始时间、结束时间
    return {
      weekNum,
      startDate: this.getFromtDateTime(startDate), // 转换为 ISO 字符串格式
      endDate: this.getFromtDateTime(endDate),
    };
  },

  /**
	 * 根据周次  获取这个周的开始结束时间

	 * @param {Object} weekNum
	 */
  getDateForCurrentWeek(weekNum: number) {
    if (weekNum) {
      const now = new Date();
      let diffMilliseconds = Math.ceil(weekNum * (7 * 24 * 60 * 60 * 1000));
      // 获取当前周数
      const year = now.getFullYear();
      let firstDayOfYear = new Date(year, 0, 1);
      let fm = firstDayOfYear.getTime();
      let weekNow = diffMilliseconds + fm;
      let wn = new Date(weekNow);
      const dayOfWeek = wn.getDay(); // 0 表示周日，1 到 6 表示周一到周六
      const startDate = new Date(wn);
      const endDate = new Date(wn);
      // 调整为周一
      startDate.setDate(startDate.getDate() - dayOfWeek + 1 - 6);
      // 调整为周日，即下周一减去一天
      endDate.setDate(endDate.getDate() - dayOfWeek + 1);

      console.log(this.getFromtDateTime(startDate), "========", this.getFromtDateTime(endDate));

      return [this.getFromtDateTime(startDate), this.getFromtDateTime(endDate)];
    } else return [];
  },
  /**
   * 将时间Date数据，格式成标准格式： 2024-12-23 00:00:00
   * @param date 
   * @returns 
   */
  getFromtDateTime(date: Date) {
    let y = date.getFullYear();
    let m = date.getMonth() + 1;
    let mm = m < 10 ? "0" + m : m;
    let d = date.getDate();
    let dd = d < 10 ? "0" + d : d;
    let h = date.getHours();
    let hh = h < 10 ? "0" + h : h;
    let M = date.getMinutes();
    let MM = M < 10 ? "0" + M : M;
    let s = date.getSeconds();
    let ss = s < 10 ? "0" + s : s;
    return `${y}-${mm}-${dd} ${hh}:${MM}:${ss}`;
  },
};
export default dateUtils;
